package service.impl;

import dao.PatientDao;
import dao.RegisterDao;
import entity.Patient;
import entity.Register;
import org.springframework.stereotype.Service;
import service.RegisterService;
import util.Random;

import javax.annotation.Resource;
import java.util.*;

@Service("registerService")
public class RegisterServiceImpl implements RegisterService {

    @Resource(name = "registerDao")
    RegisterDao registerDao;

    @Resource(name = "patientDao")
    PatientDao patientDao;
    @Override
    public Map<String, Object> GetRegisterInfo(Map<String, Object> map) {
        Map<String,Object> map1=new HashMap<String, Object>();
        map1.put("total",registerDao.getTotal(map));
        map1.put("rows",registerDao.GetRegisterInfo(map));
        return map1;
    }

    @Override
    public Patient GetPatientByRegister(String pName) {
        return registerDao.QueryPatientByRegister(pName);
    }

    @Override
    public int AddRegister(Map<String, Object> map) {
        String pdID=(String) map.get("PdId");
        System.out.println(pdID);
        if(pdID.equals("null")){
            Patient patient=(Patient)map.get("patient");
            Map<String,Object> map1=new HashMap<String, Object>();
            map1.put("pName",patient.getpName());
            map1.put("pEmal",patient.getpEmal());
            map1.put("pSex",patient.getpSex());
            map1.put("pPhone",patient.getpPhone());
            map1.put("pBirthDate",patient.getpBirthDate());
            map1.put("pCards",patient.getpCards());
            int pd =patientDao.AddPatient(map1);
            if (pd!=0){
              pdID=patientDao.GetIdBycard(patient.getpCards());
            }
        }
        Register register= (Register)map.get("register");
        Map<String,Object> map2=new HashMap<String, Object>();
        map2.put("rBalance",register.getrBalance());
        map2.put("rCode",Random.getNumberForPK());
        map2.put("rDate",new Date());
        map2.put("pId",pdID);
        return registerDao.AddRegister(map2);
    }
}
